package nl.ypmits.utils.as3.view.simpledraw {
	import nl.ypmits.utils.as3.math.SimpleMath;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 */
	public class Square extends Sprite {

		private var drawarea : Rectangle;
		private var color : uint = 0xFF9900;
		private var ratios : Array = [];
		private var matrix : Matrix;
		private var gradientcolors : Array = [];
		private var gradientalphas : Array = [];

		
		public function Square( _drawarea : Rectangle, _gradientcolors : Array = null, _gradientalphas : Array = null ) {
			if( !_drawarea ) {
				drawarea = new Rectangle( 0, 0, 100, 100 );
			} else {
				drawarea = _drawarea;
			}
			if( _gradientcolors ) {
				gradientcolors = _gradientcolors;
			}
			if( _gradientalphas ) {
				gradientalphas = _gradientalphas;
			}
			
			draw();
		}

		
		private function draw() : void {
			var i : int;
			if( gradientcolors ) {
				if( !gradientalphas ) {
					for ( i = 0; i < gradientcolors.length; i++ ) {
						gradientalphas[i] = 1-i;
					}
				}
				for ( i = 0; i < gradientcolors.length; i++ ) {
					ratios[i] = 255 * i;
				}
				matrix = new Matrix();
				matrix.createGradientBox( drawarea.height, drawarea.width, 0, 0, 0);
				matrix.rotate( SimpleMath.degreesToRadians( 90 ) );
				graphics.beginGradientFill( GradientType.LINEAR, gradientcolors, gradientalphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0 );
			} else {
				graphics.beginFill( color );
			}
			graphics.drawRect( drawarea.x, drawarea.y, drawarea.width, drawarea.height );
			graphics.endFill();
		}
	}
}
